<template>
	<div style="max-height: 150px;overflow: auto" >
		<div v-for="item in this.selectedFlowList" :key="item.runId" style="display: inline-block;">
			<el-tag style="margin-right: 10px;">
				<a @click="readBpmDetails(item.runId)" style="cursor: pointer;">NO:{{item.id}}  {{ item.flowTitle }}</a>
				<el-icon color="#409EFF" v-if="this.disabled!=true">
					<el-icon-close v-bind:disabled="this.disabled" @click="removeFlow(item.runId)"/>
				</el-icon>
			</el-tag>
		</div>
	</div>
	<el-button type="primary" icon="sc-icon-allocation" @click="selectFlow" style="margin-left: 10px" v-if="this.disabled!=true">选择</el-button>
	<el-dialog :title="`流程选择【`+(this.mode?'多选':'单选')+`】`" v-model="visible" customClass="customWidth" destroy-on-close @closed="closeDialog">
		<el-container>
			<el-header>
				<div style="width: 40%;display: inline-block;padding:0 10px 0 10px">
					<el-form-item label="流水号" style="margin-bottom: 0">
						<el-input v-model="search.bpmRunNo" placeholder="流水号" clearable maxlength="80"></el-input>
					</el-form-item>
				</div>
				<div style="width: 40%;display: inline-block;padding:0 10px 0 10px">
					<el-form-item label="流程标题" prop="isTop" style="margin-bottom: 0">
						<el-input placeholder="流程标题" v-model="this.search.keyword" clearable></el-input>
					</el-form-item>
				</div>
				<div style="width: 10%;display: inline-block">
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</el-header>
			<el-header style="height: auto;">
				<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
			</el-header>
			<scTable ref="table" :params="search" :apiObj="apiObj" row-key="runId" stripe remoteSort remoteFilter>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="流水号" prop="id" width="100"></el-table-column>
				<el-table-column label="流程名称" prop="flowTitle" width="200" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="流程状态" prop="status" width="100">
					<template #default="scope">
						<el-tag v-if="scope.row.status=='1'" type="success">已结束</el-tag>
						<el-tag v-else type="waring">进行中</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建人" prop="userName" width="100" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right">
					<template #default="scope">
						<el-button-group>
							<el-button text type="danger" icon="el-icon-delete" size="small" @click="removeFlow(scope.row.runId)" v-if="testRowSelect(scope.row)"></el-button>
							<el-button text type="success" icon="el-icon-check" size="small" @click="selectBpm(scope.row, scope.$index)" v-else></el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-container>
		<template #footer>
			<el-button @click="closeSelect">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="selectOk">确 定</el-button>
		</template>
	</el-dialog>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
export default {
	name: "Flow Select Dialog",
	emits: ['success', 'closed'],
	components: {
		scSelectFilter,
	},
	props: {
		hideAllBtn:{type:Boolean},
		resType:{type:String},//1:返回名称，同时不过行初始化
		queryType: {type: String},
		runId: {type: Array},
		mode: {type: Boolean},
		disabled:{type:Boolean}
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			disabledS:this.disabled,
			dialog: {
				isShow: false
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				}
			],
			apiObj:this.$API.bpm.bpmList.getBpmListForSelect,
			search: {
				bpmRunNo:null,
				queryType:this.queryType,
				flowId:'',
				date:'',
				keyword: null
			},
			selectedFlowList:this.runId,
			backSelectList:[],
			selectedRunList:[],
		}
	},
	created() {
		if(this.queryType=='3')
		{
			this.search.flowId = this.$route.query.flowId;
		}
	},
	mounted() {
	},
	methods:{
		testRowSelect(row)
		{
			if(this.selectedRunList.includes(row.runId))
			{
				//this.$refs.table.toggleRowSelection(row);
				return true
			}else
			{
				return false;
			}
		},
		filterChange(data){
			this.search.date = data.date;
			this.$refs.table.upData(this.search)
		},
		readBpmDetails(runId)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/handle/read',
					query: {
						runId: runId
					}
				})
			})
		},
		selectFlow()
		{
			this.selectedRunList = [];
			for (var i = 0; i < this.selectedFlowList.length; i++) {
				this.selectedRunList.push(this.selectedFlowList[i].runId);
			}
			this.dialog.isShow = true
			this.$nextTick(() => {
				this.visible = true;
				return this
			})
		},
		removeFlow(runId)
		{
			for (let i = 0; i < this.selectedFlowList.length; i++) {
				if (this.selectedFlowList[i].runId == runId) {
					this.selectedFlowList.splice(i, 1);
				}
			}
			for (let k = 0; k < this.selectedRunList.length; k++) {
				if (this.selectedRunList[k] == runId) {
					this.selectedRunList.splice(k, 1);
				}
			}
			this.$emit('update:runId', this.selectedFlowList);
		},
		selectBpm(row,index)
		{
			if(this.mode==false)
			{
				if(this.selectedFlowList.length>0)
				{
					this.$alert("单选模式不可多选！", "提示", {type: 'error'})
				}else {
					this.selectedFlowList.push(row);
				}
			}else {
				let flag=true;
				if(this.selectedFlowList=='')
				{
					this.selectedFlowList=[];
				}
				this.selectedFlowList.forEach(item=>{
					if(item.runId==row.runId)
					{
						flag = false;
					}
				})
				if(flag)
				{
					this.selectedFlowList.push(row);
					this.selectedRunList.push(row.runId);
				}
			}
			this.$refs.table.toggleRowSelection(row)
			this.$emit('update:runId', this.selectedFlowList)
		},
		selectOk() {
			this.visible = false;
			this.backSelectList = this.selectedFlowList;
			this.$emit('update:runId', this.selectedFlowList)
		},
		closeSelect() {
			this.selectedFlowList = this.backSelectList;
			this.visible = false
		},
		closeDialog() {
			this.selectedFlowList = this.backSelectList;
			this.$emit('closed')
		},
		//搜索
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
	}
}
</script>
<style>
.customWidth {
	width: 50%;
}
</style>
